package com.hyyt.shopjsp.product.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.pojo.*;
import com.hyyt.shopjsp.product.service.IShopProductTypeService;
import com.hyyt.shopjsp.product.service.IShopSpecificationService;
import com.hyyt.shopjsp.product.service.IShopSpecificationValueService;
import com.hyyt.shopjsp.redis.service.IRedisInitializingBasicDataService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘超 on 2017/8/6.
 */

@Controller
@RequestMapping("/back/specification")
public class ShopSpecificationContronller extends BaseController{

    private Logger logger = Logger.getLogger(ShopSpecificationContronller.class);
    private String basePath = "/back/shop/product/specification/";
    @Resource
    private IShopSpecificationService shopSpecificationService;
    @Resource
    private IShopSpecificationValueService shopSpecificationValueService;
    @Resource
    private IRedisInitializingBasicDataService redisInitializingBasicDataService;
    @Resource
    private IShopProductTypeService shopProductTypeService;

    /**
     * 进入商品规格页
     * @param productTypeId
     * @param param
     * @return
     */
    @RequestMapping("gotoSpecificationPage")
    public String gotoSpecificationTreePage(Integer productTypeId, Map<String,Object> param){
        String  productTypeName = redisInitializingBasicDataService.getProductTypeNameById(productTypeId);
        param.put("productTypeName",productTypeName);
        param.put("productTypeId",16);
        return basePath+"specificationList";
    }

    /**
     * 列表显示商品规格
     * @param productTypeId 商品分类ID
     * @param page
     * @param name          规格名称
     */
    @RequestMapping(value = "listSpecification",method = RequestMethod.POST)
    @ResponseBody
    public void listSpecification(String productTypeId,String name, Page page, HttpServletResponse response){
        Map<String ,Object> params = new HashMap<>();
        if(Utils.objectIsEmpty(productTypeId)){
            productTypeId = "16";
        }
        if(Utils.objectIsNotEmpty(name)){
            params.put("name",name.trim());
        }
        params.put("productTypeId",productTypeId);
        int totalRecordCount = shopSpecificationService.countShopSpecification(params);
        page.setPageInfo(10, totalRecordCount, page.getCurrentPage());
        List<Map<String,Object>> shopSpecificationList = shopSpecificationService.queryShopSpecificationByProductTypeId(params,page.getPageRecordBeginIndex(), page.getPageSize());
        String  productTypeName = redisInitializingBasicDataService.getProductTypeNameById(Integer.parseInt(productTypeId));
        for(Map<String,Object> map:shopSpecificationList){
            map.put("productTypeName",productTypeName);
        }
        params.clear();
        params.put("total", totalRecordCount);// total键 存放总记录数，必须的
        params.put("rows", shopSpecificationList);// rows键 存放每页记录 list
        ResultMsg.create(params, response);  //把内容发送到页面
    }

    /**
     * 获取商品规格和规格值
     * @param specificationId
     * @return
     */
    @RequestMapping(value="getSpecificationInfo",method= RequestMethod.POST)
    @ResponseBody
    public ResultMsg getSpecificationInfo(String specificationId){
        if(Utils.objectIsNotEmpty(specificationId)){
            ShopSpecification shopSpecification = shopSpecificationService.loadShopSpecification(Integer.parseInt(specificationId));
            ShopProductType shopProductType = shopProductTypeService.loadShopProductType(shopSpecification.getProductTypeId());
            Map<String,Object> param = new HashMap<>();
            param.put("specificationId",specificationId);
            List<ShopSpecificationValue> specificationValueList = shopSpecificationValueService.queryShopSpecificationValue(param);
            param.clear();
            param.put("shopProductType",shopProductType);
            param.put("specification",shopSpecification);
            param.put("specificationValueList",specificationValueList);
            return ResultMsg.create(true,null, FastJsonUtils.toJSONString(param));
        }
        return ResultMsg.create(false);
    }

    /**
     * 删除商品规格（只能删除商品规格下没有规格值的数据）
     * @param specificationId
     * @return
     */
    @RequestMapping(value="deleteSpecification",method=RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteSpecification(String specificationId){
        if(Utils.objectIsNotEmpty(specificationId)){
            Map<String,Object> param = new HashMap<>();
            param.put("specificationId",specificationId);
            int countSpecificationValue = shopSpecificationValueService.countShopSpecificationValue(param);
            if(countSpecificationValue==0){
                shopSpecificationService.deleteShopSpecification(Integer.parseInt(specificationId));
                return ResultMsg.create(true);
            }
        }
        return ResultMsg.create(false);
    }

    /**
     * 保存或修改商品规格
     * @param shopSpecification
     * @return
     */
    @RequestMapping(value="saveOrUpdateSpecification",method=RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateSpecification(ShopSpecification shopSpecification){
        if(Utils.objectIsNotEmpty(shopSpecification)){
            if(Utils.objectIsEmpty(shopSpecification.getSpecificationId())){
                shopSpecificationService.saveShopSpecification(shopSpecification);
            }else {
                shopSpecificationService.updateShopSpecification(shopSpecification);
            }
            return ResultMsg.create(true);
        }else{
            return ResultMsg.create(false);
        }
    }

    /**
     * 移除商品规格值
     * @param specificationValueId
     * @return
     */
    @RequestMapping(value = "deleteSpecificationValue",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteSpecificationValue(Integer specificationValueId){
        if(Utils.objectIsNotEmpty(specificationValueId)){
            shopSpecificationValueService.deleteShopSpecificationValue(specificationValueId);
            return ResultMsg.create(true);
        }
        return ResultMsg.create(false);
    }

}
